<?php
/*
 *  Maniter: A webbased business managment tool.
 *  Copyright (C) 2007  Dimitri Michaux <dimitri.michaux@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * lists.php, Maniter
 * 
 * Displays a list of all the purchaseorders/offers splited up
 * over several pages and tabs.
 *
 * @author Dimitri Michaux
 * @version 0.1
 * @package maniter_core
 */

require_once( "lib-common.php" );

if( isset($_GET['action']) ) {
	if( $_GET['action'] == "delete" ) {
		if( isset($_GET['id']) ) {
			$order = $_DB->getPurchaseOrder( $_GET['id'] );
			$order->remove();
			$redirectTo = "index.php";
			include( $_TEMPLATES['redirect'] );
			exit;
		}
	}
}

//What page are we on?
//for purchaseorders
if( isset($_GET['por']) && is_numeric($_GET['por']) )
  $pageOr = $_GET['por'];
else
  $pageOr = 1;
  
//for purchaseoffers
if( isset($_GET['pof']) && is_numeric($_GET['pof']) )
  $pageOf = $_GET['pof'];
else
  $pageOf = 1;

//what tab is active?
if( isset($_GET['tab']) && ($_GET['tab']=='of'||$_GET['tab']=='or') )
  $tab = $_GET['tab'];
else
  $tab = 'or';

//navigation for puchaseorders
$pagenav = makePagenave( $pageOr, ceil( DB::get()->count('purchase_orders') / 25 ), 'por', '&pof='.$pageOf.'&tab=or'  );

//get purchase orders
$items =& PurchaseOrder::getPurchaseOrders( ($pageOr-1)*25, 25 );

//create list of purchase orders
ob_start();
include( 'templates/list.tpl' );
$tpl->addTab( 'Facturen', ob_get_contents(), $tab=='or'?true:false );
ob_end_clean();

//navigation for puchaseoffers
$pagenav = makePagenave( $pageOf, ceil( DB::get()->count('purchase_offers') / 25 ), 'pof', '&por='.$pageOr.'&tab=of'  );

//get purchase offers
$items =& PurchaseOffer::getPurchaseOffers( ($pageOf-1)*25, 25 );
//create list of purchase offers
ob_start();
include( 'templates/list.tpl' );
$tpl->addTab( 'Offertes', ob_get_contents(), $tab=='of'?true:false );
ob_end_clean();

$tpl->setTitle( 'Lijsten' );
$tpl->output();

function makePagenave( $page, $pages, $par='p', $pars='' )
{ 
  $pagenav = '';
  if( $page > 1 )
    $pagenav.= '<a href="lists.php?' . $par .'=' . ($page-1) . $pars . '">&lt;</a> ';
  else
    $pagenav.= '&lt; ';
  
  for( $i=1 ; $i <= $pages ; $i++ )
  {
    if( $page == $i )
      $pagenav.= $i . ' ';
    else
      $pagenav.= '<a href="lists.php?'. $par .'=' . $i . $pars . '">' . $i . '</a> ';
  }
  
  if( $page < $pages  )
    $pagenav.= '<a href="lists.php?' . $par .'=' . ($page+1) . $pars . '">&gt;</a> '; //don't know why
  else
    $pagenav.= '&gt;';
  
  return $pagenav;
}
?>
